package database;

import java.util.ArrayList;
import java.util.List;

import usercontrol.device;
import usercontrol.user;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import database.DatabaseUtility.DatabaseHelper;

public class DeviceProfileDAO {
	private static final String DATABASE_NAME = "FTICO_DATABASE";
	private static final int DATABASE_VERSION = 1;
	private static final String DEVICE_PROFILE = "DEVICE_PROFILE";
	private static final String DEVICE_ID = "DEVICE_ID";
	private static final String DEVICE_NAME = "DEVICE_NAME";
	private static final String DEVICESERIALNUMBER = "DEVICESERIALNUMBER";
	private static final String DEVICEMACADDRESS = "DEVICEMACADDRESS";
	private static final String DEVICE_TYPE = "DEVICE_TYPE";
	private static final String CREATE_AT = "CREATE_AT";
	private static final String UPDATE_AT = "UPDATE_AT";
	public static final String DELETE_FLG = "DELETE_FLG";

	private Context mContext;
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDB;

	public DeviceProfileDAO(Context ctx) {
		mContext = ctx;
	}

	public DeviceProfileDAO open() {
		mDbHelper = new DatabaseHelper(mContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDbHelper.set_mContext(mContext);
		mDB = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void update() {
		mDbHelper.onUpgrade(mDB, DATABASE_VERSION, 2);
	}

	public int checkDeviceByName(String device_name) {
		int count = 0;
		Cursor cursor = mDB.query(DEVICE_PROFILE, null, "DEVICE_NAME = '"
				+ device_name + "' AND DELETE_FLG = 0", null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				count++;
			} while (cursor.moveToNext());
		}
		cursor.close();
		return count;
	}

	public void insertDevice(String device_name, String serial_number,
			String mac_address, int type, String createAt, String updateAt) {
		ContentValues values = new ContentValues();
		if (checkDeviceByName(device_name) == 0) {
			values.put(DEVICE_NAME, device_name);
			values.put(DEVICESERIALNUMBER, serial_number);
			values.put(DEVICEMACADDRESS, mac_address);
			values.put(DEVICE_TYPE, type);
			values.put(CREATE_AT, createAt);
			values.put(UPDATE_AT, updateAt);
			values.put(DELETE_FLG, 0);
			mDB.insert(DEVICE_PROFILE, null, values);
		}
	}

	public device getDeviceByID(int device_id) {
		device _device = new device();
		Cursor cursor = mDB.query(DEVICE_PROFILE, null, "DEVICE_ID = "
				+ device_id + " AND DELETE_FLG = 0", null, null, null, null);
		if (cursor.moveToFirst()) {
			_device.setId(Integer.parseInt(cursor.getString(0)));
			_device.setDevice(cursor.getString(1));
			_device.setDeviceSerialNumber(cursor.getString(2));
			_device.setDeviceMacAddress(cursor.getString(3));
			_device.set_deviceType(Integer.parseInt(cursor.getString(4)));
			_device.setCreateAt(cursor.getString(5));
			_device.setUpdateAt(cursor.getString(6));
		}
		cursor.close();
		return _device;
	}
	public List<String> getAllDeviceName() {
		List<String> _deviceListName = new ArrayList<String>();
		Cursor cursor = mDB.query(DEVICE_PROFILE, null, "DELETE_FLG = 0", null,
				null, null, null);
		if (cursor.moveToFirst()) {
			do {
				_deviceListName.add(cursor.getString(1));
			} while (cursor.moveToNext());
		}
		return _deviceListName;
	}
	public List<device> getAllDevice() {
		List<device> _deviceList = new ArrayList<device>();
		Cursor cursor = mDB.query(DEVICE_PROFILE, null, "DELETE_FLG = 0", null,
				null, null, null);
		if (cursor.moveToFirst()) {
			do {
				device _device = new device();
				_device.setId(Integer.parseInt(cursor.getString(0)));
				_device.setDevice(cursor.getString(1));
				_device.setDeviceSerialNumber(cursor.getString(2));
				_device.setDeviceMacAddress(cursor.getString(3));
				_device.set_deviceType(Integer.parseInt(cursor.getString(4)));
				_device.setCreateAt(cursor.getString(5));
				_device.setUpdateAt(cursor.getString(6));
			} while (cursor.moveToNext());
		}
		return _deviceList;
	}
}
